import 'package:boxes_flutter/flutter/slc/common/num_util.dart';
import 'package:boxes_flutter/flutter/slc/common/text_util.dart';

/**
 * @author: slc
 * @date: 2025/03/07
 * 基于NumUtil拓展
 */
class SlcNumUtil {
  /// The parameter [fractionDigits] must be an integer satisfying: `0 <= fractionDigits <= 20`.
  static num? getNumByValueStr(String? valueStr, {int? fractionDigits}) {
    if (TextUtil.isEmpty(valueStr)) {
      return null;
    }
    return NumUtil.getNumByValueStr(valueStr!, fractionDigits: fractionDigits);
  }

  /// get int by value str.
  static int? getIntByValueStr(String? valueStr, {int? defValue}) {
    if (TextUtil.isEmpty(valueStr)) {
      return defValue;
    }
    return NumUtil.getIntByValueStr(valueStr!, defValue: defValue);
  }

  /// get double by value str.
  static double? getDoubleByValueStr(String? valueStr, {double? defValue}) {
    if (TextUtil.isEmpty(valueStr)) {
      return defValue;
    }
    return NumUtil.getDoubleByValueStr(valueStr!, defValue: defValue);
  }

  /// 输入一个数字不足指定长度的字符串，在前或后面补0
  static String? padZeroByValue(int? valueInt, {bool padRight = true, int? length}) {
    String valueStr = valueInt.toString();
    return padZeroByValueStr(valueStr, padRight: padRight, length: length);
  }

  /// 输入一个字符串不足指定长度的字符串，在前或后面补0
  static String? padZeroByValueStr(String? valueStr, {bool padRight = true, int? length}) {
    if (TextUtil.isEmpty(valueStr)) {
      return null;
    }
    if (length == null) {
      return valueStr;
    }
    if (valueStr!.length >= length) {
      return valueStr;
    }
    if (padRight) {
      return valueStr.padRight(length, '0');
    }
    return valueStr.padLeft(length, '0');
  }
}
